package com.yuyu.shop.infrastructure.persistence.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuyu.shop.domain.order.DO.OrderDO;
import com.yuyu.shop.domain.order.repository.SecKillRepository;
import com.yuyu.shop.infrastructure.persistence.convert.SecKillOrderPOConvert;
import com.yuyu.shop.infrastructure.persistence.mapper.SecKillOrderMapper;
import com.yuyu.shop.infrastructure.persistence.po.SecKillOrderPO;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author
 * @version 1.0
 * @description: TODO
 * @date 2022/9/23 16:11
 */
@Service
@AllArgsConstructor(onConstructor = @_(@Autowired))
public class SecKillRepositoryImpl implements SecKillRepository {

    private final SecKillOrderMapper secKillOrderMapper;


    @Override
    public Boolean insert(OrderDO orderDO) {
        SecKillOrderPO secKillOrderPO = SecKillOrderPOConvert.DO2PO(orderDO);
        SecKillOrderPO orderPO1 = secKillOrderMapper.selectOne(new LambdaQueryWrapper<SecKillOrderPO>().eq(SecKillOrderPO::getOrderId, orderDO.getOrderId()));
        if (Objects.nonNull(orderPO1)){
            return Boolean.FALSE;
        }
        int insert = secKillOrderMapper.insert(secKillOrderPO);
        return insert>0 ?Boolean.TRUE:Boolean.FALSE;
    }

    @Override
    public OrderDO getByUserId(Long userId, Long goodId) {
        SecKillOrderPO secKillOrderPO = secKillOrderMapper.selectOne(new LambdaQueryWrapper<SecKillOrderPO>()
                .eq(Objects.nonNull(userId), SecKillOrderPO::getUserId, userId)
                .eq(Objects.nonNull(goodId), SecKillOrderPO::getGoodId, goodId));
       return SecKillOrderPOConvert.PO2DO(secKillOrderPO);
    }

    @Override
    public Boolean updateById(OrderDO orderDO) {
        SecKillOrderPO secKillOrderPO = SecKillOrderPOConvert.DO2PO(orderDO);
        int insert = secKillOrderMapper.updateById(secKillOrderPO);
        return insert>0 ?Boolean.TRUE:Boolean.FALSE;
    }
}
